package com.hdd.hll.modules.order.service.impl;


import com.hdd.hll.common.response.Response;
import com.hdd.hll.common.response.ResponseUtils;
import com.hdd.hll.common.utils.StringUtils;
import com.hdd.hll.modules.info.mapper.CoalMapper;
import com.hdd.hll.modules.info.mapper.CompanyMapper;
import com.hdd.hll.modules.order.bean.PlanOrder;
import com.hdd.hll.modules.order.bean.PlanOrderAll;
import com.hdd.hll.modules.order.mapper.PlanMapper;
import com.hdd.hll.modules.order.service.PlanService;
import com.hdd.hll.modules.order.web.dto.AddPlandDto;
import com.hdd.hll.modules.order.web.dto.PlanDto;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
@Slf4j
public class PlanServiceImpl implements PlanService {


    @Autowired
    RedissonClient redissonClient;

    @Autowired
    PlanMapper planMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    CoalMapper coalMapper;


    @Override
    public  List<PlanOrderAll> findPlans() {
        RLock lock = redissonClient.getLock("Plan"+1);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PlanOrderAll> planOrders = planMapper.findPlans();
                return planOrders;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }



    @Override
    public List<PlanOrder> findPlans(PlanDto planDto){
        RLock lock = redissonClient.getLock("Plan"+2);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PlanOrder> planOrders = planMapper.findPlansByMaxId(planDto);
                return planOrders;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }


    @Override
    public Response<Integer> addPlans(AddPlandDto dto) {
        RLock lock = redissonClient.getLock("Plan"+3);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                String data =  planMapper.findByCode(dto.getPlanCode());
                if(StringUtils.isNullOrEmpty(data)){
                    // 验证发货方 收货方 煤种是否存在
                    String collectId = companyMapper.findByID(dto.getCollectID());
                    if(StringUtils.isNullOrEmpty(collectId)){
                        return ResponseUtils.initsByCode("common.data.no.p",",当前所传id为："+dto.getCollectID());
                    }

                    String provideId = companyMapper.findByID(dto.getProvideID());
                    if(StringUtils.isNullOrEmpty(provideId)){
                        return ResponseUtils.initsByCode("common.data.no.c",",当前所传id为："+dto.getProvideID());
                    }

                    String coalID = coalMapper.findByID(dto.getCoalID());
                    if(StringUtils.isNullOrEmpty(coalID)){
                        return ResponseUtils.initsByCode("common.data.no.coal",",当前所传id为："+dto.getCoalID());
                    }

                   // 计划类型：1.采购计划 2.销售计划
                    if(dto.getPlanType().equals(2)){
                        dto.setDeptID(dto.getProvideID());
                    }else {
                        dto.setDeptID(dto.getCollectID());
                    }
                    dto.setCreationTime(new Date());
                    Integer num = planMapper.insert(dto);
                    return ResponseUtils.initsByInts(num);
                }else {
                    return ResponseUtils.initsByCode("common.data.repeat");
                }
            } finally {
                lock.unlock();
            }
        }
        return ResponseUtils.initsFail();
    }

}
